﻿const query = require('../client');
const moment = require('moment');
const createGuid = require('../lib/util').createGuid;
const dateToString = require('../lib/util').dateToString;

module.exports = {
    // 获取某个班级的成绩环节列表
    getListByClassGuid: (classGuid, callback) => {
        let sql = `SELECT dss.PK_ScoreSegmentGuid,dss.\`Name\` AS ScoreSegmentName,ciss.ScoreSegmentWeight
            FROM class_in_scoresegment AS ciss JOIN d_scoresegment AS dss ON dss.PK_ScoreSegmentGuid=ciss.FK_ScoreSegmentGuid 
            WHERE ciss.\`Status\`<99 AND dss.\`Status\`<99 AND ciss.FK_ClassGuid='${classGuid}'`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, vals);
        });
    },
    // 获取某个成绩环节的某个成绩子项
    get: (scoreSegmentGuid, scoreSubItemGuid, callback) => {
        let sql = `SELECT ssissi.*,ciss.ScoreSegmentWeight,dss.\`Name\` AS ScoreSegmentName,dssi.IconUrl AS ScoreSubItemIconUrl,
            dssi.BackgroundColor AS ScoreSubItemBackgroundColor,dssit.\`Name\` AS ScoreSubItemTypeName,dssit.IconUrl AS ScoreSubItemTypeIconUrl 
            FROM class_in_scoresegment AS ciss JOIN scoresegment_in_scoresubitem AS ssissi ON ciss.FK_ScoreSegmentGuid=ssissi.FK_ScoreSegmentGuid 
            JOIN d_scoresegment AS dss ON dss.PK_ScoreSegmentGuid=ssissi.FK_ScoreSegmentGuid 
            JOIN d_scoresubitem AS dssi ON dssi.PK_ScoreSubItemGuid=ssissi.FK_ScoreSubItemGuid 
            JOIN d_scoresubitemtype AS dssit ON dssit.PK_ScoreSubItemTypeGuid=dssi.FK_ScoreSubItemTypeGuid 
            WHERE ciss.\`Status\`<99 AND ssissi.\`Status\`<99 AND dss.\`Status\`<99 AND dssi.\`Status\`<99 AND dssit.\`Status\`<99 
            AND ciss.FK_ClassGuid='${classGuid}' 
            ORDER BY ssissi.CreateTime DESC,ssissi.ID`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, vals);
        });
    },
    // 保存权重
    saveWeight: function(params, ip, callback){
        if (params.length > 0)
        {
            let sql = ``;
            params.forEach(segment=>{
                sql += `UPDATE class_in_scoresegment SET ScoreSegmentWeight=${segment.ScoreSegmentWeight},FK_ModifierGuid=null,ModifyTime=SYSDATE(),ModifyIP='${ip}' WHERE FK_ClassGuid='${segment.ClassGuid}' AND FK_ScoreSegmentGuid='${segment.ScoreSegmentGuid}';`;
            });
            sql = sql.substring(0, sql.length - 1);
            query(sql, function(err, vals) {
                if (err) {
                    console.log(err);
                    callback(0, err);
                    return;
                }
                callback(1, vals);
            });
        }
        else
        {
            callback(1, {});
        }
    },
    // 初始化班级和成绩环节
    initClassScoreSegment: function(classGuid, ip, callback){
        let sqlstr = `INSERT INTO class_in_scoresegment(PK_ClassInScoreSegmentGuid,FK_ClassGuid,FK_ScoreSegmentGuid,ScoreSegmentWeight,Status,CreateTime,CreateIP) 
            SELECT UUID(),'${classGuid}',PK_ScoreSegmentGuid,TemplateWeight,1,SYSDATE(),'${ip}' FROM d_scoresegment WHERE Status<99`;
        query(sqlstr, function(err, vals){
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, vals);
        });
    }
};